﻿'********************************************************************************
'*
'*      着工/完了履歴　ベースフォーム
'*
'*      File Name : frmIcTagEditor.vb
'*      Title     : ICタグ中身編集画面
'*      機能    　：ICタグ中身を編集する
'*
'********************************************************************************
'----------------------------------<<修正履歴>>----------------------------------
'ﾊﾞｰｼﾞｮﾝ   日付       作業者        ｺﾒﾝﾄ
'--------- ---------- ------------ -----------------------------------------------
' V1.0.0   2016/09/03 S.Yu         新規作成
' V3.0.0   2018/01/30 佐川         北京対応
'********************************************************************************

Imports ICtagControl.VBStrings
Imports System.Threading.Thread

Public Class frmIcTagEditor

    'Public g_cRFID As clsRfidTag

    Private IcTag As frmStartCompleteBase.TagData
    Private IcTagOld As frmStartCompleteBase.TagData
    Protected m_fTagWriteFailed As Boolean = False
    'Private m_fIcTagUsed As Boolean
    Protected LastWriteTag As New frmStartCompleteBase.TagData
    ' >> Add 安部 2018/2/4 iniファイル整理
    'Protected htMsgMst As New Hashtable                ' エラーメッセージ保持
    ' << Add 安部 2018/2/4 
    Protected m_cLotInfo As New clsDbLotInfo

    '>> 20180330 大賀 No128 IcTag編集画面履歴の作成
    ' 変更メモの最大長
    Private Const TextLogMaxLength = 1024
    ' 読込時のタグデータ
    Private ReadTagData As New frmStartCompleteBase.TagData

    Public RackUpdateFlg As Integer = 0 '１の場合、ラックNo.変更ボタンで呼び出す'ADD 苗 2018/4/13
    '<< 20180330 大賀 No128 IcTag編集画面履歴の作成

    'ICタグ着工状態
    Public Enum TagProcessStatus
        Ready
        Processing
        ManufComplete
    End Enum


    Private Sub frmIcTagEditor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        InitStatus()

        'メッセージ取得
        '>> 2018/02/20 安部 iniファイル整理
        'setAllErrMsg()
        '<< 2018/02/20 安部 iniファイル整理

        'm_fIcTagUsed = CBool(GetIniFile("SERIAL", "IcTagUsed", "False", g_szInitFileName))
        'Dim m_szPortName As String = GetIniFile("SERIAL", "PortName", "COM10", g_szInitFileName)


        If m_fIcTagUsed Then
            Try
                '>>20170714
                If IsNothing(g_cRFID) Then

                    g_cRFID = New clsRfidTag()
                End If
                g_cRFID.BeginSerial(m_szPortName, AddressOf RecieveData)
                g_cRFID.AddSerialEventHandler(AddressOf DataReceive)

                g_cRFID.CheckTag()
            Catch ex As Exception
                g_cRFID = Nothing
                'Me.Close()
                m_fIcTagUsed = False
                'Exit Sub
            End Try

            '<<20170714
        End If

        btnReturn.DialogResult = Windows.Forms.DialogResult.Cancel

        ' 数値入力テキストボックスのイベント設定
        setHandlerIntegerTextBox(txtInput)
        setHandlerIntegerTextBox(txtFinish)
        setHandlerIntegerTextBox(txtDefect)
        setHandlerIntegerTextBox(txtFrame)
        setHandlerIntegerTextBox(txtCross)
        setHandlerIntegerTextBox(txtCrossPcb)

        ' 文字列入力テキストボックスのイベント設定
        setHandlerStringTextBox(txtLotNo)
        setHandlerStringTextBox(txtDifLot)
        setHandlerStringTextBox(txtEquipID)
        setHandlerStringTextBox(txtProcCD)

        setHandlerStringTextBox(txtRack1)
        setHandlerStringTextBox(txtRack2)
        setHandlerStringTextBox(txtRack3)
        setHandlerStringTextBox(txtRack4)
        setHandlerStringTextBox(txtRack5)


        '>>2018/2/16 大賀 No74 ICタグ編集項目追加対応
        setHandlerIntegerTextBox(txtEmptyChip)

        setHandlerStringTextBox(txtPltEquipNo)
        '<<2018/2/16 大賀 No74 ICタグ編集項目追加対応
        '>>20180302 大賀 GroupKey入力時ウェハ枚数入力
        setHandlerIntegerTextBox(txtWaferNum)
        '<<20180302 大賀 GroupKey入力時ウェハ枚数入力

        '組立KeyNoを非活性にする
        txtLotNo.Enabled = False

    End Sub

    '>>2018/2/16 大賀 No74 ICタグ編集項目追加対応
    Private Sub setHandlerIntegerTextBox(ByVal setTextBox As TextBox)
        AddHandler setTextBox.GotFocus, AddressOf txt_GotFocus
        AddHandler setTextBox.KeyPress, AddressOf txtInteger_KeyPress
        AddHandler setTextBox.LostFocus, AddressOf txtInteger_LostFocus
    End Sub
    Private Sub setHandlerStringTextBox(ByVal setTextBox As TextBox)
        AddHandler setTextBox.GotFocus, AddressOf txt_GotFocus
        AddHandler setTextBox.KeyPress, AddressOf txtString_KeyPress
        AddHandler setTextBox.LostFocus, AddressOf txtString_LostFocus 'ADD 苗 2018/4/13
    End Sub
    '<<2018/2/16 大賀 No74 ICタグ編集項目追加対応

    Private Sub frmIcTagEditor_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If m_fIcTagUsed AndAlso Not IsNothing(g_cRFID) Then
            g_cRFID.ClearData()
            g_cRFID.DisableHandShake()
            g_cRFID.Disconnect()
            g_cRFID.EndSerial()
        End If
    End Sub

    Private Sub InitStatus()
        Dim dt As New DataTable
        dt.Columns.Add("INDEX")
        dt.Columns.Add("NAME")

        dt.Rows.Add(CInt(TagProcessStatus.Ready), "着工等待") ' UPD 苗  2018/3/20
        dt.Rows.Add(CInt(TagProcessStatus.Processing), "着工中") ' UPD 苗  2018/3/20
        dt.Rows.Add(CInt(TagProcessStatus.ManufComplete), "制造完成") ' UPD 苗  2018/3/20

        cmbStatus.DataSource = dt
        cmbStatus.ValueMember = "INDEX"
        cmbStatus.DisplayMember = "NAME"


        Dim dtOnhold As New DataTable
        dtOnhold.Columns.Add("INDEX")
        dtOnhold.Columns.Add("NAME")
        dtOnhold.Rows.Add(CInt(frmStartCompleteBase.OnHoldStatus.Normal), "通常") ' UPD 苗  2018/3/20
        dtOnhold.Rows.Add(CInt(frmStartCompleteBase.OnHoldStatus.OnHold), "保留") ' UPD 苗  2018/3/20
        dtOnhold.Rows.Add(CInt(frmStartCompleteBase.OnHoldStatus.Solved), "解除") ' UPD 苗  2018/3/20

        cmbOnHold.DataSource = dtOnhold
        cmbOnHold.ValueMember = "INDEX"
        cmbOnHold.DisplayMember = "NAME"

        'ADD 苗 2018/4/13 BEGIN
        If Me.RackUpdateFlg = 1 Then
            For n = 0 To Me.Controls.Count - 1
                Me.Controls(n).Enabled = False
            Next
            btnLotNo.Enabled = True
            txtLotNo.Enabled = True
            txtWorker.Enabled = True
            grpRack.Enabled = True
            btnWrite.Enabled = True
            btnReturn.Enabled = True
            btnCreateRackInfo.Enabled = True
        End If
        'ADD 苗 2018/4/13 END
    End Sub

    Public Sub DataReceive(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)
        Dim serialPort As IO.Ports.SerialPort = CType(sender, IO.Ports.SerialPort)
        Static Dim lstByte As New List(Of Byte)
        Static Dim m_fDataEnded As Boolean
        Static Dim SerialDataProcs As New Delegate_TagDataProcess(AddressOf TagDataProcess)

        g_SerialArg = e

        Try
            While (serialPort.BytesToRead > 0)
                While (serialPort.BytesToRead > 0)

                    Dim byteTmp As Byte = serialPort.ReadByte
                    If byteTmp = clsRfidTag.CR Then 'And lstByte.Item(lstByte.Count - 2) = clsRfidTag.ETX Then
                        lstByte.Add(byteTmp)
                        m_fDataEnded = True
                        Exit While
                    End If
                    lstByte.Add(byteTmp)
                End While

                If m_fDataEnded Then

                    '読取ったデータの整理
                    If lstByte.Count >= clsRfidTag.MIN_BYTE + DATA_LENGTH Then
                        g_fDataOK = True

                        'TagDataProcess(lstByte.ToArray)
                        Invoke(SerialDataProcs, lstByte.ToArray)
                    ElseIf lstByte.Count > 0 Then

                        If OK.Equals(frmStartCompleteBase.BytesToString(lstByte.ToArray)) Then
                            g_fRespOK = True
                            If lstByte(4) = &HA1 Then
                                m_fTagWriteFailed = False
                            End If
                        Else
                            g_fRespOK = False
                            g_fNACK = True

                            If frmStartCompleteBase.BytesToString(lstByte.ToArray).StartsWith("ERR") Then
                                m_fTagWriteFailed = True
                            End If
                        End If
                    End If
                    lstByte.Clear()
                    m_fDataEnded = False
                End If

            End While
        Catch ex As Exception
            MsgBox(ex.Message)
            'Me.Close()
            'End If
        End Try

    End Sub

    Public Sub RecieveData()
        Static Dim lstByte As New List(Of Byte)
        Static Dim lngCheckSum As Long = 0
        Static Dim fChecked As Boolean = False
        Static Dim m_fDataEnded As Boolean
        'シリアルポートが受信している間はほかの処理に割込ませないように
        'SyncLock Me

        'Debug.Print(10)

        Try
            While (g_SerialPort.BytesToRead > 0)
                While (g_SerialPort.BytesToRead > 0)
                    'Invoke(New Delegate_SetTimer(AddressOf SetTimer), False)

                    Dim byteTmp As Byte = g_SerialPort.ReadByte
                    If lstByte.Count >= clsRfidTag.MIN_BYTE - 2 Then
                        If fChecked Then
                            If byteTmp = clsRfidTag.CR And lstByte.Item(lstByte.Count - 2) = clsRfidTag.ETX Then
                                lstByte.Add(byteTmp)
                                m_fDataEnded = True
                                Exit While
                            Else
                                fChecked = False
                            End If
                        ElseIf lstByte.Item(lstByte.Count - 1) = clsRfidTag.ETX AndAlso byteTmp = CByte(lngCheckSum And CLng(&HFF)) Then
                            fChecked = True
                        Else
                            lngCheckSum += byteTmp
                        End If
                    Else
                        lngCheckSum += byteTmp
                    End If
                    lstByte.Add(byteTmp)
                    If lstByte.Count = clsRfidTag.MIN_BYTE + clsRfidTag.UID_LENGTH + 112 Then
                        g_SerialPort.DiscardInBuffer()
                        lstByte.Clear()
                        Sleep(100)
                        m_fDataEnded = True
                        Exit While
                        '    lstByte.Clear()
                        '    Invoke(New Delegate_SetTimer(AddressOf SetTimer), True)
                        '    Exit Sub
                    End If
                End While

                'Sleep(10)

                If m_fDataEnded Then

                    '読取ったデータの整理
                    If lstByte.Count >= clsRfidTag.MIN_BYTE + DATA_LENGTH Then
                        'g_fRespOK = True
                        g_fDataOK = True
                        TagDataProcess(lstByte.ToArray)
                    ElseIf lstByte.Count > 0 Then
                        If lstByte.Count = 17 Then
                            g_fNoTagResp = True
                        End If

                        If lstByte(2) = &H30 Then
                            g_fRespOK = True
                            If lstByte(4) = &HA1 Then
                                m_fTagWriteFailed = False
                            End If
                        Else
                            g_fRespOK = False
                            If lstByte(4) = &H4 Then
                                m_fTagWriteFailed = True
                            End If
                        End If
                    End If
                    '処理状態を元に戻す
                    lstByte.Clear()
                    fChecked = False
                    m_fDataEnded = False
                    lngCheckSum = 0
                End If

            End While
        Catch ex As Exception
            'If Not (m_fTagSet) Then
            MsgBox(ex.Message)
            'End If
        End Try
    End Sub

    Protected Delegate Sub Delegate_TagDataProcess(ByVal data() As Byte)
    'タグから取得したデータを処理する
    Protected Sub TagDataProcess(ByVal byteData() As Byte)
        Dim tagData = frmStartCompleteBase.TagData.Initalize
        With frmStartCompleteBase
            Try
                Dim m_cData As New clsDbCondData

                Dim szLot As String = ""
                If CByte(clsRfidTag.CR) = byteData(byteData.Length - 1) Then
                    szLot = .BytesToString(byteData.ToArray).Replace(clsRfidTag.CR.ToString, "")
                End If

                'l_lotdataよりデータを取得
                Dim dtData = m_cData.GetWbLotData(szLot)
                If dtData.Rows.Count > 0 Then
                    '>>2018/2/16 大賀 No74 ICタグ編集項目追加対応
                    tagData.setData(dtData)
                    '<<2018/2/16 大賀 No74 ICタグ編集項目追加対応

                    IcTag = tagData
                    ReadTagData = IcTag '20180330 大賀 No128 IcTag編集画面履歴の作成
                    GetDataFromTag()
                    'tagTimer.Start()
                Else
                    MsgBox(getMsgStr("MSG10011", txtLotNo.Text))
                    GetDataFromTag()
                End If

            Catch ex As Exception
                tagData = New frmStartCompleteBase.TagData
            End Try
        End With

    End Sub

    '画面項目に値を設定
    Private Sub GetDataFromTag()
        With IcTag
            txtLotNo.Text = .szLotNo
            txtDifLot.Text = .szDifLotNo
            If .Status = "-2147483648" Then .Status = "0"
            cmbStatus.SelectedIndex = CInt(.Status)
            txtEquipID.Text = .szEquipID
            txtProcCD.Text = .szProcsCD
            txtInput.Text = .iInput.ToString
            txtFinish.Text = .iFinish.ToString
            If .iFailed.ToString = "-2147483648" Then .iFailed = 0
            txtDefect.Text = .iFailed.ToString
            txtFrame.Text = .iFrame.ToString
            txtCross.Text = .iCross.ToString
            txtCrossPcb.Text = .iCrossFrm.ToString
            cbQflag.Checked = .fQflag
            cbTestFlag.Checked = .fTestflag
            cbCheckFlag.Checked = .fCheckflag
            cmbOnHold.SelectedIndex = CInt(.OnHoldSt)
            txtRack1.Text = .container1
            txtRack2.Text = .container2
            txtRack3.Text = .container3
            txtRack4.Text = .container4
            txtRack5.Text = .container5
            '>>2018/2/16 大賀 No74 ICタグ編集項目追加対応
            cbFrameFlag.Checked = .frameIDFlag
            txtEmptyChip.Text = .emptyChipNum.ToString
            txtPltEquipNo.Text = .plateEquipNo
            '<<2018/2/16 大賀 No74 ICタグ編集項目追加対応
            '>>20180302 大賀 GroupKey入力時ウェハ枚数入力
            txtWaferNum.Text = .waferNum.ToString
            '<<20180302 大賀 GroupKey入力時ウェハ枚数入力
            '>>20180319 大賀 No104 WBラック管理テーブルのクリア処理を追加
            If .szProcsCD.Equals("S42") Then
                btnWBTableClear.Enabled = True
            Else
                btnWBTableClear.Enabled = False
            End If
            '<<20180319 大賀 No104 WBラック管理テーブルのクリア処理を追加
        End With

    End Sub

    Protected Sub TagWriteBack(ByVal tagDat As frmStartCompleteBase.TagData, Optional ByVal fIsRewrite As Boolean = False)
        Static fIsRetry As Boolean
        'Dim fTimer As Boolean = TimerTag.Enabled
        Dim dataWrite As New List(Of Byte)
        '組立Lot No
        Dim szLotNo As String = FixCharSpace(tagDat.szLotNo, LOT_NO_LENGTH)
        dataWrite.AddRange(frmStartCompleteBase.StringToBytes(szLotNo))
        '拡散Lot No
        Dim szDifLot As String = FixCharSpace(tagDat.szDifLotNo, DIF_LOT_LENGTH)
        dataWrite.AddRange(frmStartCompleteBase.StringToBytes(szDifLot))
        '状態
        dataWrite.Add(CInt(tagDat.Status))
        '装置ID
        Dim szEquipID As String = FixCharSpace(tagDat.szEquipID, EQUIP_NO_LENGTH)
        dataWrite.AddRange(frmStartCompleteBase.StringToBytes(szEquipID))
        '工程コード
        Dim szProcsCD As String = FixCharSpace(tagDat.szProcsCD, PROCS_CD_LENGTH)
        dataWrite.AddRange(frmStartCompleteBase.StringToBytes(szProcsCD))
        '投入数
        dataWrite.AddRange(frmStartCompleteBase.Int16ToBytes(tagDat.iInput))
        '完成数
        dataWrite.AddRange(frmStartCompleteBase.Int16ToBytes(tagDat.iFinish))
        '不良数
        dataWrite.AddRange(frmStartCompleteBase.Int16ToBytes(tagDat.iFailed))
        'フレーム数
        dataWrite.AddRange(frmStartCompleteBase.Int16ToBytes(tagDat.iFrame))
        '×印数
        dataWrite.AddRange(frmStartCompleteBase.Int16ToBytes(tagDat.iCross))
        '×印枚数
        dataWrite.AddRange(frmStartCompleteBase.Int16ToBytes(tagDat.iCrossFrm))
        'Qフラグ
        dataWrite.Add(CByte(CBool(tagDat.fQflag)))
        'Testフラグ
        dataWrite.Add(CByte(tagDat.fTestflag))
        'Checkフラグ
        dataWrite.Add(CByte(CBool(tagDat.fCheckflag)))
        '保留フラグ
        dataWrite.Add(CByte(tagDat.OnHoldSt))
        'ダミーバイト
        Dim iDummy As Integer = dataWrite.Count Mod 4
        While iDummy > 0
            dataWrite.Add(CByte(0))
            iDummy -= 1
        End While

        'UIDを指定したタグにデータを書込む
        fIsRetry = True
        If Not fIsRewrite Then
            LastWriteTag = tagDat
            fIsRetry = False
        End If

        m_fTagWriteFailed = True
        g_cRFID.ClearData()
        If g_cRFID.WriteData(dataWrite.ToArray, tagDat.szUID) = clsRfidTag.WriteError.OK Then
        ElseIf fIsRewrite Then
            IcTagWriteFailed()
        Else
            TagWriteBack(LastWriteTag, True)
        End If

    End Sub

    Private Sub IcTagWriteFailed()

        'Select Case MsgBox("ICタグ書込みは失敗しました。ID tag数据保存失败。", MsgBoxStyle.RetryCancel)’DEL 苗 2018/3/20
        Select Case MsgBox("ID tag数据保存失败。", MsgBoxStyle.RetryCancel)' ADD 苗  2018/3/20
            Case MsgBoxResult.Retry
                TagWriteBack(LastWriteTag, True)
            Case MsgBoxResult.Ignore

        End Select
    End Sub


    Private Sub txtString_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        '頭に付いている'%'を消す
        Static Dim fIsPercentInput As Boolean = False
        If e.KeyChar = CChar("%") OrElse e.KeyChar = CChar("％") OrElse e.KeyChar = CChar("％") Then
            fIsPercentInput = True
            e.Handled = True
            Exit Sub
        End If
        If fIsPercentInput Then
            fIsPercentInput = False
            e.Handled = True
            Exit Sub
        End If
    End Sub

    Private Sub txtInteger_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If e.KeyChar <> CChar(vbBack) Then
            If Asc(e.KeyChar) < &H30 Or Asc(e.KeyChar) > &H39 Then
                e.Handled = True
                Exit Sub
            End If
        End If
    End Sub

    Private Sub txtInteger_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim txtInteger As TextBox = CType(sender, TextBox)
        If txtInteger.TextLength <= 0 Then
            txtInteger.Text = "0"
        End If
    End Sub

    Private Sub txt_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim txtBox As TextBox = CType(sender, TextBox)
        txtBox.SelectAll()
    End Sub

    '>> 20180402 大賀 No128 IcTag編集画面履歴の作成
    Private Function isUpdate() As Boolean
        If IcTag.szLotNo <> ReadTagData.szLotNo Then
            Return True
        End If
        If IcTag.szDifLotNo <> ReadTagData.szDifLotNo Then
            Return True
        End If
        If IcTag.Status <> ReadTagData.Status Then
            Return True
        End If
        If IcTag.szEquipID <> ReadTagData.szEquipID Then
            Return True
        End If
        If IcTag.szProcsCD <> ReadTagData.szProcsCD Then
            Return True
        End If
        If IcTag.iInput <> ReadTagData.iInput Then
            Return True
        End If
        If IcTag.iFinish <> ReadTagData.iFinish Then
            Return True
        End If
        If IcTag.iFailed <> ReadTagData.iFailed Then
            Return True
        End If
        If IcTag.iFrame <> ReadTagData.iFrame Then
            Return True
        End If
        If IcTag.iCross <> ReadTagData.iCross Then
            Return True
        End If
        If IcTag.iCrossFrm <> ReadTagData.iCrossFrm Then
            Return True
        End If
        If IcTag.fQflag <> ReadTagData.fQflag Then
            Return True
        End If
        If IcTag.fTestflag <> ReadTagData.fTestflag Then
            Return True
        End If
        If IcTag.fCheckflag <> ReadTagData.fCheckflag Then
            Return True
        End If
        If IcTag.OnHoldSt <> ReadTagData.OnHoldSt Then
            Return True
        End If
        If IcTag.container1 <> ReadTagData.container1 Then
            Return True
        End If
        If IcTag.container2 <> ReadTagData.container2 Then
            Return True
        End If
        If IcTag.container3 <> ReadTagData.container3 Then
            Return True
        End If
        If IcTag.container4 <> ReadTagData.container4 Then
            Return True
        End If
        If IcTag.container5 <> ReadTagData.container5 Then
            Return True
        End If
        If IcTag.emptyChipNum <> ReadTagData.emptyChipNum Then
            Return True
        End If
        If IcTag.plateEquipNo <> ReadTagData.plateEquipNo Then
            Return True
        End If
        If IcTag.frameIDFlag <> ReadTagData.frameIDFlag Then
            Return True
        End If
        If IcTag.waferNum <> ReadTagData.waferNum Then
            Return True
        End If
        Return False
    End Function
    '<< 20180402 大賀 No128 IcTag編集画面履歴の作成

    Private Sub btnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrite.Click

        If txtLotNo.TextLength <= 0 Then
            '  MsgBox("IDタグをリーダーに設置してください、" & vbCrLf & "或いはデータを入力してください。")
            MsgBox("请Leader进行设置、" & vbCrLf & "或者请输入数据。")
            Exit Sub
        End If

        resetRackBackColor()

        '入力チェック
        If Not isInputCheck() Then
            MsgBox(getMsgStr("MSG10017"))
            Exit Sub
        End If
        '>>20180313 大賀 搬送容器桁数チェックを共通化
        '桁数チェック
        If Not isLengthCheck() Then
            MsgBox(getMsgStr("MSG10033"))
            Exit Sub
        End If
        '<<20180313 大賀 搬送容器桁数チェックを共通化
        '重複チェック
        If Not isAllCheckRackDuplicate() Then
            MsgBox(getMsgStr("MSG00064", "Rack CASE"))
            Exit Sub
        End If

        Dim m_cIdou As New clsDbIdoHyo 'ADD 苗 2018/3/29

        '>> 20180330 大賀 No128 IcTag編集画面履歴の作成
        If String.IsNullOrEmpty(txtWorker.Text) Then
            MsgBox(getMsgStr("MSG00002", lblWorker.Text))
            txtWorker.BackColor = Color.Red
            txtWorker.Focus()
            Exit Sub
        Else
            txtWorker.BackColor = Color.White
        End If

        'ADD 苗 2018/3/29 begin
        If Me.RackUpdateFlg = 0 Then 'ADD 苗 2018/4/13
            If String.IsNullOrEmpty(txtPW.Text) Then
                MsgBox(getMsgStr("MSG00002", Label20.Text))
                txtPW.BackColor = Color.Red
                txtPW.Focus()
                Exit Sub
            Else
                txtPW.BackColor = Color.White
            End If
        End If
        'ADD 苗 2018/3/29 end

        If txtMemo.Text.Length > TextLogMaxLength Then
            MsgBox(getMsgStr("MSG00002", lblMemo.Text))
            txtMemo.BackColor = Color.Red
            txtMemo.Focus()
            Exit Sub
        Else
            txtMemo.BackColor = Color.White
        End If
        '<< 20180330 大賀 No128 IcTag編集画面履歴の作成

        If Me.RackUpdateFlg = 1 Or txtPW.Text.Equals(m_cIdou.getPassword) Then 'ADD 苗 2018/3/29　'UPD 苗 2018/4/13 

            Me.Enabled = False

            With IcTag
                .szLotNo = txtLotNo.Text
                .szDifLotNo = txtDifLot.Text
                .Status = cmbStatus.SelectedIndex
                .szEquipID = txtEquipID.Text
                .szProcsCD = txtProcCD.Text
                .iInput = txtInput.Text
                .iFinish = txtFinish.Text
                .iFailed = txtDefect.Text
                .iFrame = txtFrame.Text
                .iCross = txtCross.Text
                .iCrossFrm = txtCrossPcb.Text
                .fQflag = cbQflag.Checked
                .fTestflag = cbTestFlag.Checked
                .fCheckflag = cbCheckFlag.Checked
                .OnHoldSt = cmbOnHold.SelectedIndex
                .container1 = txtRack1.Text
                .container2 = txtRack2.Text
                .container3 = txtRack3.Text
                .container4 = txtRack4.Text
                .container5 = txtRack5.Text
                '>>2018/2/16 大賀 No74 ICタグ編集項目追加対応
                .emptyChipNum = txtEmptyChip.Text
                .plateEquipNo = txtPltEquipNo.Text
                .frameIDFlag = cbFrameFlag.Checked
                '<<2018/2/16 大賀 No74 ICタグ編集項目追加対応
                '>>20180302 大賀 GroupKey入力時ウェハ枚数入力
                .waferNum = txtWaferNum.Text
                '<<20180302 大賀 GroupKey入力時ウェハ枚数入力
            End With

            '>> 20180402 大賀 No128 IcTag編集画面履歴の作成
            If Not isUpdate() Then
                MsgBox(getMsgStr("MSG10037"))
                Me.Enabled = True
                Exit Sub
            End If
            '<< 20180402 大賀 No128 IcTag編集画面履歴の作成

            '>> 20180330 大賀 No128 IcTag編集画面履歴の作成
            Dim optTime = New clsDbCondData().GetServerTime()
            Dim idInfo = New clsDbIdoHyo()
            idInfo.GetEquipInfoByID(g_CURRENT_EQUIP_ID)
            Dim equipNo = idInfo.EquipInfo.szEquipNo

            DBacs.BeginTransaction()
            Dim isError = DbWrite(optTime, equipNo)

            If isError Then
                DBacs.RollbackTransaction()
                MsgBox(getMsgStr("MSG00008", txtLotNo.Text))
                InitDisplay()
            Else    '>>20180319 大賀 No110 書き込み成功時にダイアログ表示
                DBacs.CommitTransaction()
                MsgBox(getMsgStr("MSG00047"))
                txtPW.Clear() 'ADD 苗 2018/3/29
                ReadTagData = IcTag
            End If
            '<< 20180330 大賀 No128 IcTag編集画面履歴の作成

            Me.Enabled = True

        Else
            'ADD 苗 2018/3/29 Begin
            MsgBox(getMsgStr("MSG00004", Label20.Text))
            txtPW.Focus()
            txtPW.SelectAll()
        End If
        'ADD 苗 2018/3/29 END
    End Sub

    Private Function DbWrite(optTime As Date, equipNo As String) As Boolean
        Dim cnt As Integer
        cnt = New clsDbLotInfo().SaveLotInfoTagHistory(ReadTagData, clsDbLotInfo.CHANGE_TYPE.BEFORE, optTime, txtWorker.Text, equipNo, System.Net.Dns.GetHostName(), txtMemo.Text)
        If cnt = 0 Then
            Return True
        End If

        cnt = New clsDbLotInfo().SaveLotInfoTagHistory(IcTag, clsDbLotInfo.CHANGE_TYPE.AFTER, optTime, txtWorker.Text, equipNo, System.Net.Dns.GetHostName(), txtMemo.Text)
        If cnt = 0 Then
            Return True
        End If

        cnt = New clsDbLotInfo().SaveLotInfoTagEditor(IcTag)
        If cnt = 0 Then
            Return True
        End If
        Return False
    End Function

    Private Sub txtLotNo_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtLotNo.KeyPress
        '頭に付いている'%'を消す
        Static Dim fIsPercentInput As Boolean = False
        If e.KeyChar = CChar("%") OrElse e.KeyChar = CChar("％") OrElse e.KeyChar = CChar("％") Then
            fIsPercentInput = True
            e.Handled = True
            Exit Sub
        End If
        If fIsPercentInput Then
            fIsPercentInput = False
            e.Handled = True
            Exit Sub
        End If

        If e.KeyChar = vbCr Then
            Dim tagData = frmStartCompleteBase.TagData.Initalize
            txtLotNo.Text = txtLotNo.Text.ToUpper

            '組立ロットNo.にてデータ取得
            Dim dtData As DataTable = New clsDbCondData().GetWbLotData(Me.txtLotNo.Text)
            If dtData.Rows.Count > 0 Then
                InitDisplay()
                '>>2018/2/16 大賀 No74 ICタグ編集項目追加対応
                tagData.setData(dtData)
                '<<2018/2/16 大賀 No74 ICタグ編集項目追加対応
                IcTag = tagData
                ReadTagData = IcTag     '20180330 大賀 No128 IcTag編集画面履歴の作成
                GetDataFromTag()

                grpRack.Enabled = True
                'btnLoadRack_Click(btnLoadRack, New EventArgs)
            Else
                MsgBox(getMsgStr("MSG10011", txtLotNo.Text))
                InitDisplay()
            End If
        Else
            'ClearRack()
            'grpRack.Enabled = False
        End If
    End Sub

    Private Sub btnLotNo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLotNo.Click
        'g_cRFID.CheckTag()
        'If Not String.IsNullOrEmpty(txtLotNo.Text) Then
        '    txtLotNo_KeyPress(txtLotNo, New System.Windows.Forms.KeyPressEventArgs(vbCr))
        'End If

        txtLotNo.Enabled = True
        txtLotNo.Focus()
        txtLotNo.SelectAll()
    End Sub

    Private dtPre As DataTable

    Private Sub SetRow(ByRef dtPre As DataTable, ByVal szRack As String, ByVal szTag As String, Optional ByVal szSeq As Integer = 1)
        Dim row() As DataRow
        row = dtPre.Select("MNF_CND_TAG='" & szTag & "' AND SEQ='" & szSeq & "'")
        If row.Length > 0 Then
            row(0)("ACTUAL_MODEL") = szRack
        ElseIf Not String.IsNullOrEmpty(szRack) Then
            Dim newRow = dtPre.NewRow
            newRow("TYPE") = 3
            newRow("MNF_CND_TAG") = szTag
            newRow("SEQ") = szSeq
            newRow("ACTUAL_MODEL") = szRack
            dtPre.Rows.Add(newRow)
        End If
    End Sub

    Private Sub ClearRack()
        Dim ctrls() As Control = grpRack.Controls.Cast(Of Control).Where(Function(ctrl) GetType(TextBox).Equals(ctrl.GetType)).ToArray
        For Each ctrl In ctrls
            CType(ctrl, TextBox).Clear()
        Next
    End Sub

    '組立KeyNoタブキー対応
    Private Sub lotNo_PreviewKeyDown(ByVal sender As Object, ByVal e As PreviewKeyDownEventArgs) Handles txtLotNo.PreviewKeyDown
        'タブキーが押下された場合は、改行コードと見なし処理を行う
        If e.KeyCode = Keys.Tab Then
            e.IsInputKey = True
            txtLotNo_KeyPress(sender, New KeyPressEventArgs(vbCr))
        End If
    End Sub

    '>>2018/2/16 大賀 No79 エラー判定表示方法追加
    ''' <summary>
    ''' CASE重複チェック
    ''' </summary>
    ''' <returns></returns>
    Private Function isAllCheckRackDuplicate() As Boolean
        Dim retVal = True
        For Each ctl In grpRack.Controls
            If TypeOf ctl Is TextBox And Not String.IsNullOrEmpty(ctl.Text) Then
                If False = checkRackDuplicate(ctl) Then
                    retVal = False
                End If
            End If
        Next
        isAllCheckRackDuplicate = retVal
    End Function
    Private Function checkRackDuplicate(ByVal target As TextBox) As Boolean
        checkRackDuplicate = True
        For Each grpCtl In grpRack.Controls
            If TypeOf grpCtl Is TextBox And Not String.IsNullOrEmpty(grpCtl.Text) Then
                If Not target.Name.Equals(grpCtl.Name) Then
                    If target.Text.Equals(grpCtl.Text) Then
                        target.BackColor = Color.Red
                        grpCtl.BackColor = Color.Red
                        checkRackDuplicate = False
                    End If
                End If
            End If
        Next
    End Function

    ''' <summary>
    ''' CASE入力チェック時の背景色設定
    ''' </summary>
    ''' <param name="base">開始テキストボックス</param>
    Private Sub setRackInputErrorColor(ByVal base As TextBox)
        Dim startIndex = base.Name.Substring(base.Name.Length - 1)
        For i As Integer = CInt(startIndex) To 5
            Dim data = grpRack.Controls.Find(String.Format("txtRack{0}", i), False)(0)
            If String.IsNullOrEmpty(data.Text) Then
                data.BackColor = Color.Red
            End If
        Next
    End Sub

    ''' <summary>
    ''' ラックケースの背景色を戻す
    ''' </summary>
    Private Sub resetRackBackColor()
        For Each ctl In grpRack.Controls
            If TypeOf ctl Is TextBox Then
                ctl.BackColor = Color.White
            End If
        Next
    End Sub
    '<<2018/2/16 大賀 No79 エラー判定表示方法追加

    '>>20180313 大賀 搬送容器桁数チェックを共通化
    ''' <summary>
    ''' ラックケース入力の文字列長チェック処理
    ''' </summary>
    ''' <returns></returns>
    Private Function isLengthCheck() As Boolean
        Dim result As Boolean = True
        For i As Integer = 1 To 5
            Dim data = grpRack.Controls.Find(String.Format("txtRack{0}", i), False)(0)
            If Not String.IsNullOrEmpty(data.Text) Then
                If data.Text.Length <> frmStartCompleteBase.WAFER_CASE_NO_LENGTH AndAlso data.Text.Length <> frmStartCompleteBase.RACK_CASE_NO_LENGTH AndAlso data.Text.Length <> frmStartCompleteBase.MS_RACK_CASE_NO_LENGTH Then
                    data.BackColor = Color.Red
                    result = False
                End If
            End If
        Next
        Return result
    End Function
    '<<20180313 大賀 搬送容器桁数チェックを共通化

    'CASE入力チェック
    Private Function isInputCheck() As Boolean
        Dim result As Boolean = True
        'CASE1空白チェック
        If txtRack1.Text.Equals("") Then
            'CASE1が空白でCASE2～5に入力がある場合はエラー
            If Not txtRack2.Text.Equals("") Or Not txtRack3.Text.Equals("") Or Not txtRack4.Text.Equals("") Or Not txtRack5.Text.Equals("") Then
                result = False
                setRackInputErrorColor(txtRack1)
            End If
        ElseIf txtRack2.Text.Equals("") Then
            'CASE2が空白でCASE3～5に入力がある場合はエラー
            If Not txtRack3.Text.Equals("") Or Not txtRack4.Text.Equals("") Or Not txtRack5.Text.Equals("") Then
                result = False
                setRackInputErrorColor(txtRack2)
            End If
        ElseIf txtRack3.Text.Equals("") Then
            'CASE3が空白でCASE4,5に入力がある場合はエラー
            If Not txtRack4.Text.Equals("") Or Not txtRack5.Text.Equals("") Then
                result = False
                setRackInputErrorColor(txtRack3)
            End If
        ElseIf txtRack4.Text.Equals("") Then
            'CASE4が空白でCASE5に入力がある場合はエラー
            If Not txtRack5.Text.Equals("") Then
                result = False
                setRackInputErrorColor(txtRack4)
            End If
        End If

        Return result
    End Function

    '画面の初期化
    Private Sub InitDisplay()

        txtLotNo.Text = String.Empty
        txtDifLot.Text = String.Empty
        cmbStatus.SelectedIndex = CInt(0)
        txtEquipID.Text = String.Empty
        txtProcCD.Text = String.Empty
        txtInput.Text = 0
        txtFinish.Text = 0
        txtDefect.Text = 0
        txtFrame.Text = 0
        txtCross.Text = 0
        txtCrossPcb.Text = 0
        cbQflag.Checked = False
        cbTestFlag.Checked = False
        cbCheckFlag.Checked = False
        cmbOnHold.SelectedIndex = CInt(0)
        txtRack1.Text = String.Empty
        txtRack2.Text = String.Empty
        txtRack3.Text = String.Empty
        txtRack4.Text = String.Empty
        txtRack5.Text = String.Empty
        '>>2018/2/16 大賀 No74 ICタグ編集項目追加対応
        txtEmptyChip.Text = String.Empty
        txtPltEquipNo.Text = String.Empty
        cbFrameFlag.Checked = False
        '<<2018/2/16 大賀 No74 ICタグ編集項目追加対応
        '>>20180302 大賀 GroupKey入力時ウェハ枚数入力
        txtWaferNum.Text = String.Empty
        '<<20180302 大賀 GroupKey入力時ウェハ枚数入力
        '>> 20180330 大賀 No128 IcTag編集画面履歴の作成
        txtWorker.Text = String.Empty
        txtWorker.BackColor = Color.White
        txtPW.Text = String.Empty
        txtPW.BackColor = Color.White
        txtMemo.Text = String.Empty
        txtMemo.BackColor = Color.White
        '<< 20180330 大賀 No128 IcTag編集画面履歴の作成

        txtLotNo.Enabled = False
    End Sub

    '>>20180319 大賀 No104 WBラック管理テーブルのクリア処理を追加
    Private Sub btnWBTableClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWBTableClear.Click
        If MsgBox(getMsgStr("MSG10034"), MsgBoxStyle.YesNo) = MsgBoxResult.No Then
            Exit Sub
        End If
        Dim cDb = New clsDBWbRackMng
        putlog("IDTagEdit", txtLotNo.Text & "-" & txtWorker.Text & "-点击【WB料盒清空】") '2020/8/8 ver.58
        cDb.delOldData(txtLotNo.Text)
        MsgBox(getMsgStr("MSG10035"))
    End Sub
    Private Sub putlog(Type As String, msg As String)
        Dim m_cLog As New clsIcTagLog
        Dim log As New frmStartCompleteBase.Log

        log.szOperator = Type
        log.szMsg = msg
        With log
            .szEquipID = g_EQUIP_NO 'UPD 苗 2018/1/19 装置No.の対応
            .szProcCD = txtProcCD.Text
            .szLotNo = txtLotNo.Text
            .dteOpt = DateTime.Now
            .szControl = "按钮点击"
        End With

        m_cLog.PutDbLog(log)
    End Sub

    'ADD 苗 2018/4/13 BEGIN
    Private Sub txtString_LostFocus(sender As Object, e As EventArgs)
        sender.text = sender.text.ToString.ToUpper
    End Sub

    Private Sub btnCreateRackInfo_Click(sender As Object, e As EventArgs) Handles btnCreateRackInfo.Click
        Dim objlotDefect As New clsDbLotDefect
        Try
            If txtProcCD.Text = "S41" Then
                objlotDefect.insertRackInfo(txtDifLot.Text, txtProcCD.Text, txtRack1.Text, txtRack2.Text, txtRack3.Text, txtRack4.Text, txtRack5.Text)

            Else
                objlotDefect.insertRackInfo(txtLotNo.Text, txtProcCD.Text, txtRack1.Text, txtRack2.Text, txtRack3.Text, txtRack4.Text, txtRack5.Text)

            End If
            putlog("IDTagEdit", txtLotNo.Text & "-" & txtWorker.Text & "-" & txtProcCD.Text & "-点击RackInfo作成") '2020/8/8 ver.58

            MsgBox(getMsgStr("MSG00047"))
        Catch ex As Exception
            MsgBox(getMsgStr("MSG00123"))
        End Try

    End Sub
    'ADD 苗 2018/4/13 END
    '<<20180319 大賀 No104 WBラック管理テーブルのクリア処理を追加

End Class